<?php

class DbMembersModel extends DAO_MySql {

	/**
	 * 要操作的表名
	 *
	 * @var string
	 */
	private static $_table = 'member_list';

	/**
	 * 主键
	 *
	 * @var string
	 */
	private static $_pk = 'memberId';

	/**
	 * 允许的字段
	 *
	 * @var array
	 */
	private static $_allowed = array(
		'memberId',
		'memberName',
		'loginName',
		'password',
		'email',
		'mobile',
		'createTime',
	);

	/**
	 * 必须的字段
	 *
	 * @var array
	 */
	private static $_required = array(
		'memberName',
		'password',
		'createTime',
	);

	private static $_json = array();

	/**
	 * add
	 * 添加一条记录
	 *
	 */
	public static function addData($data, $shardKey = array()) {

		foreach (self::$_json as $field) {
			if (isset($data[$field])) {
				$data[$field] = json_encode($data[$field]);
			}
		}

		return parent::add(self::$_table, $data, self::$_required, self::$_allowed, $shardKey);
	}

	/**
	 * remove
	 * 删除一条记录
	 *
	 */
	public static function removeData($sId, $shardKey = array()) {

		return parent::remove(self::$_table, self::$_pk, $sId, $shardKey);
	}

	/**
	 * update
	 * 更新一条记录
	 *
	 */
	public static function updateData($id, $data, $shardKey = array()) {

		if ($data[self::$_pk]) {
			unset($data[self::$_pk]);
		}

		foreach (self::$_json as $field) {
			if (isset($data[$field])) {
				$data[$field] = json_encode($data[$field]);
			}
		}

		return parent::update(self::$_table, $data, self::$_pk, $id, self::$_allowed, $shardKey);
	}

	/**
	 * get
	 * 获取一条记录
	 *
	 */
	public static function getData($id, $fields = array(), $shardKey = array()) {

		if ($fields) {
			$result = array_diff($fields, self::$_allowed);
			if ($result) {
				throw new InvalidArgumentException("Invalid Field(" . join(',', $result) . ")");
			}
		} else {
			$fields = self::$_allowed;
		}

		$conditions = self::$_pk . ' = ?';
		$params = array($id);

		$data = parent::find(self::$_table, $conditions, $params, $fields, $shardKey);

		if ($data) {
			$decodeField = array_intersect($fields, self::$_json);
			if ($decodeField) {
				foreach ($decodeField as $field) {
					$data[$field] = json_decode($data[$field], true);
				}
			}
		}

		return $data;
	}


	/**
	 * listAll
	 * 列出所有记录
	 *
	 */
	public static function listAll(&$pageOptions = array(), $fields = array(), $shardKey = array()) {

		if ($fields) {
			$result = array_diff($fields, self::$_allowed);
			if ($result) {
				throw new InvalidArgumentException("Invalid Field(" . join(',', $result) . ")");
			}
		} else {
			$fields = self::$_allowed;
		}

		$data = parent::findAll(self::$_table, null, array(), $pageOptions, $fields, array('createTime' => 'DESC'), $shardKey);

		$decodeField = array_intersect($fields, self::$_json);
		if ($decodeField) {
			foreach ($data as $key => $siteInfo) {
				foreach ($decodeField as $field) {
					$data[$key][$field] = json_decode($siteInfo[$field], true);
				}
			}
		}

		return $data;
	}

	/**
	 * countAll
	 * 获取所有记录总数
	 *
	 * @param array $shardKey 分库关键字
	 * @return integer
	 */
	public static function countAll($shardKey = array()) {
		return parent::count(self::$_table, null, array(), $shardKey);
	}

	public static function getByLoginName($loginName, $shardKey = array()) {
		return parent::findBySql(self::$_table, "SELECT * FROM table WHERE `loginName`=?", array($loginName), $shardKey);
	}
}
